// pages/headTask/index.js
const common = require('../../utils/common');
const userUtils = require("../../utils/user");
const env = require('../../utils/env');
const wxp = require('../../utils/wxp.js');

const {
  getOrderRewardInfo,
  getOrderRewardApply
} = require('../../services/orderReward')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    bgUrl: env.imgUrl + 'group_activity_bg.png',
    taskInfo: null,
    pageScrollTop: 0,
    containerTouchmove: '',
    isRulesViewShow: false,
    timerObj: { //倒计时
      timer: null,
      isEnd: false,
      endTime: '',
      item: null
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu({});
    //获取数据
    this.requestToGetOrderRewardInfo();
  },

  onUnload: function () {
    if (this.data.timerObj.timer) {
      clearTimeout(this.data.timerObj.timer);
    }
  },
  // 开始限时抢购计时器
  startTimer: function (expire) {
    let tmpTimerObj = this.data.timerObj;
    tmpTimerObj.endTime = new Date().getTime() + expire * 1000;
    this.setData({
      timerObj: tmpTimerObj,
    });
    this.countDownTimer();
  },
  // 计时器**********
  countDownTimer() { //倒计时函数
    //定时清除
    clearTimeout(this.data.timerObj.timer);

    // 获取当前时间，同时得到活动结束时间数组
    let newTime = new Date().getTime();
    var tmpTimerObj = this.data.timerObj;
    let endTime = tmpTimerObj.endTime;;

    // 对结束时间进行处理渲染到页面
    let obj = null;
    // 如果活动未结束，对时间进行处理
    if (endTime - newTime > 0) {
      let time = (endTime - newTime) / 1000;
      // 获取天、时、分、秒
      let day = parseInt(time / (60 * 60 * 24));
      let dayHour = (day > 0) ? day * 24 : 0;
      let hou = dayHour + parseInt(time % (60 * 60 * 24) / 3600);
      let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
      let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
      obj = {
        day: common.timeFormat(day),
        hou: common.timeFormat(hou),
        min: common.timeFormat(min),
        sec: common.timeFormat(sec)
      }
    } else { //活动已结束，全部设置为'00'
      obj = {
        day: '00',
        hou: '00',
        min: '00',
        sec: '00'
      }
      tmpTimerObj.isEnd = true;
    }
    tmpTimerObj.item = obj;
    //1s调用一次
    var that = this;
    var timerItem = setTimeout(function () {
      that.countDownTimer()
    }, 1000);

    if (tmpTimerObj.isEnd) {
      clearTimeout(timerItem);
      return;
    } else {
      tmpTimerObj.timer = timerItem;
    }
    // 渲染，然后每隔一秒执行一次倒计时函数
    this.setData({
      timerObj: tmpTimerObj
    })
  },
  // 限时抢购计时器**********
  //获取数据
  requestToGetOrderRewardInfo() {
    getOrderRewardInfo().then(res => {
      //set data
      this.setData({
        taskInfo: res.data
      });
      if (this.data.taskInfo.orderRewardVo.status == 1) {
        // 开启转发
        wx.showShareMenu({});
        // 开启计时器
        this.startTimer(this.data.taskInfo.orderRewardVo.escapeTime);
      }
    }).catch(err => {
      common.log(err);
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return this.shareAppMessage();
  },

  //获取分享的AppMessage
  shareAppMessage: function () {
    let tmpPath = '/pages/promotePage/index?token=' + userUtils.getToken() + '&code=' + userUtils.getCode() + "&isGroupOwner=0";
    return {
      title: this.data.taskInfo.orderRewardVo.shareTile,
      path: tmpPath,
      imageUrl: this.data.taskInfo.orderRewardVo.shareImg
    }
  },

  // 监听用户滑动页面事件
  onPageScroll: function (e) {
    var topY = e.scrollTop + "px";
    this.setData({
      pageScrollTop: topY
    });
  },

  //按钮点击事件
  //禁止滚动
  preventTouchMove: function () {},

  // 活动规则
  onRules() {
    this.setData({
      isRulesViewShow: true,
      containerTouchmove: 'preventTouchMove'
    })
  },
  // 取消规则弹窗
  onRulesBg() {
    this.setData({
      isRulesViewShow: false,
      containerTouchmove: ''
    })
  },
  // 一个步骤领取
  onOneStepDlq() {
    this.requestToApplyReward({
      step: 1
    });
  },
  // 二个步骤第一步
  onTwoStepOneDlq() {
    this.requestToApplyReward({
      step: 1
    });
  },
  // 二个步骤第二步
  onTwoStepTwoDlq() {
    this.requestToApplyReward({
      step: 2
    });
  },
  // 三个步骤第一步
  onThreeStepOneDlq() {
    this.requestToApplyReward({
      step: 1
    });
  },
  // 三个步骤第二步
  onThreeStepTwoDlq() {
    this.requestToApplyReward({
      step: 2
    });
  },
  // 三个步骤第三步
  onThreeStepThreeDlq() {
    this.requestToApplyReward({
      step: 3
    });
  },
  //领取奖励
  requestToApplyReward({
    step
  }) {
    let params = {
      step: step
    }
    getOrderRewardApply(params).then(res => {
      //
      this.handleRewardApplyData(step);
    }).catch(err => {
      common.log(err);
    });
  },
  // 处理领取奖励的状态
  handleRewardApplyData(step) {
    let tmpInfo = this.data.taskInfo;
    //只有一个阶段
    if (this.data.taskInfo.orderRewardVo.step == 1) {
      tmpInfo.orderRewardVo.stepOneStatus = 3;
      this.setData({
        taskInfo: tmpInfo
      });
    } else if (this.data.taskInfo.orderRewardVo.step == 2) {
      if (step == 1) {
        tmpInfo.orderRewardVo.stepOneStatus = 3;
        this.setData({
          taskInfo: tmpInfo
        });
      } else if (step == 2) {
        tmpInfo.orderRewardVo.stepTwoStatus = 3;
        this.setData({
          taskInfo: tmpInfo
        });
      }
    } else if (this.data.taskInfo.orderRewardVo.step == 3) {
      if (step == 1) {
        tmpInfo.orderRewardVo.stepOneStatus = 3;
        this.setData({
          taskInfo: tmpInfo
        });
      } else if (step == 2) {
        tmpInfo.orderRewardVo.stepTwoStatus = 3;
        this.setData({
          taskInfo: tmpInfo
        });
      } else if (step == 3) {
        tmpInfo.orderRewardVo.stepThreeStatus = 3;
        this.setData({
          taskInfo: tmpInfo
        });
      }
    }
  },
  //待完成
  onDaiwc() {
    wxp.showToast({
      title: '还未达成目标数量哦~'
    });
  }
})
